// pages/life/comment/comment.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		dyn: {},
		comments: [],
		//写评论的遮掩层
		show: false,
		//评论内容
		message: '',
		//父级评论id
		pid: '',
		//父级评论userid
    puser: '',
    loadModal:true
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		var json = options.dynamic;
		var obj = JSON.parse(json)
		console.log(obj)
		this.setData({
			dyn: obj
		})
	},


	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getAllComments()
	},
	giveALike() {
		var dynamic = this.data.dyn
		app.request({
			url: app.globalData.apiUrl + '/dynamic/dianzan/' + dynamic.id,
			method: 'POST',
		}).then(res => {
			if (res.statusCode == 200) {
				dynamic.liked = 1;
				dynamic.likes = dynamic.likes + 1;
				this.setData({
					dyn: dynamic
				})
			} else {
				Notify('服务错误，稍后重试');
			}
		})
	},
	//取消赞
	cancelLike() {
		var dynamic = this.data.dyn
		app.request({
			url: app.globalData.apiUrl + '/dynamic/cancel/' + dynamic.id,
			method: 'POST',
		}).then(res => {
			if (res.statusCode == 200) {
				dynamic.liked = 0;
				dynamic.likes = dynamic.likes - 1
				this.setData({
					dyn: dynamic
				})
			} else {
				Notify('服务错误，稍后重试');
			}
		})
	},
	getAllComments() {
		app.request({
			url: app.globalData.apiUrl + '/comment/1/' + this.data.dyn.id,
			method: 'GET',
		}).then(res => {
			if (res.statusCode == 200) {
				let temp = res.data.data;
				for (var i in temp) {
					if (temp[i].pid != null || '') {
						let temp_index = this.getArrayItemIndex(temp, temp[i].pid)
						temp[i].pbody = temp[temp_index].body
					}
				}
				console.log(temp)
				this.setData({
          comments: temp,
          loadModal:false
				})
				console.log(this.data.comments)
			} else {
        this.setData({
          loadModal:false
        })
				Notify('服务错误，稍后重试');
			}
		})
	},
	//操作写普通级评论的弹出层
	showPopup() {
		this.setData({
			show: true
		});
	},
	//写子级评论弹出层，需要接收父级评论参数
	showPopup2(e) {
		console.log(e)
		this.setData({
			show: true,
			pid: e.target.dataset.pid,
			puser: e.target.dataset.puser
		});
	},
	onClose() {
		this.setData({
			show: false,
			message: '',
			pid: '',
			puser: ''
		});
	},
	//提交评论
	commit() {
		let comment = JSON.stringify({
			type: 1,
			textId: this.data.dyn.id,
			body: this.data.message,
			puser: this.data.puser,
			pid: this.data.pid,
			createTime: Date.parse(new Date())
		})
		let num=this.data.comments.length+1
		console.log(num)
		app.request({
			url: app.globalData.apiUrl + '/comment/dynamic',
			method: 'POST',
			data: {
				comment: comment,
				num:num
			}
		}).then(res => {
			this.onClose()
			if (res.statusCode == 200) {
				this.getAllComments()
			} else {
				loadModel: false
				Notify('服务错误，稍后重试');
			}
		})
	},
	 //获取数组中某个元素属性的下标
	 getArrayItemIndex(arr, id) {
    var index = -1;
    if (typeof Array.prototype.findIndex === 'function') {
      index = arr.findIndex(function (item) {
        return item.id === id;
      });
    } else {
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].id === id) {
          index = i;
          break;
        }
      }
    }
    return index;
  },
})